package zjua4r.googlemap.demo;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
	 public static final String BASE_URL="http://192.168.1.100/android.ashx";
	 //获得GET请求request
	 public static HttpGet getHttpGet(String url){
	  HttpGet request=new HttpGet(url);
	  return request;
	 }
	 //获得Post请求对象request
	 public static HttpPost getHttpPost(String url){
	  HttpPost request=new HttpPost(url);
	  return request;
	 }
	 //根据请求获得响应对象response
	 public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{
	  HttpResponse response=new DefaultHttpClient().execute(request);
	  return response;
	 }
	 //根据请求获得响应对象response
	 public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{
	  HttpResponse response=new DefaultHttpClient().execute(request);
	  return response;
	 }
	 //发送Post请求,获得响应查询结果
	 public static String queryStringForPost(String url){
	  //根据url获得HttpPost对象
	  HttpPost request=HttpUtil.getHttpPost(url);
	  String result=null;
	  
	  try {
	   //获得响应对象
	   HttpResponse response = HttpUtil.getHttpResponse(request);
	   //判断是否请求成功
	   if(response.getStatusLine().getStatusCode()==200){
	    result=EntityUtils.toString(response.getEntity());
	   }
	  } catch (ClientProtocolException e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	   result="网络异常！";
	  } catch (IOException e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	   result="网络异常！";
	  }
	  return result;
	 }
	 
	 public static String queryStringForPost(HttpPost request){
	  String result=null;
	  try {
	   HttpResponse response=HttpUtil.getHttpResponse(request);
	   if(response.getStatusLine().getStatusCode()==200){
	    result=EntityUtils.toString(response.getEntity());
	   }
	  } catch (ClientProtocolException e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	   result="网络异常！";
	  } catch (IOException e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	   result="网络异常！";
	  }
	  return result;
	 }
	 public static String queryStringForGet(String url){
	  HttpGet request=HttpUtil.getHttpGet(url);
	  String result=null;
	  try {
	   HttpResponse response=HttpUtil.getHttpResponse(request);
	   if(response.getStatusLine().getStatusCode()==200){
	    result=EntityUtils.toString(response.getEntity());
	   }
	  } catch (ClientProtocolException e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	   result="网络异常！";
	  } catch (IOException e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	   result="网络异常！";
	  }
	  return result;
	 }
	 
}
